
package com.vitas.coin.util;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.OnPermissionPageCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.vitas.coin.ui.dialog.TipDlg;

import java.util.List;

public class PermissionsUtils {



    /**
     * 检查单个权限
     *
     * @param context    Context
     * @param permission 权限
     * @return boolean
     */
    public static boolean checkPermission(Context context, String permission) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }
        if (permission == null) {
            return false;
        }
        return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
    }

    /**
     * 检查多个权限
     * 检查权限
     *
     * @param permissions 权限数组
     * @param context     Context
     * @return true 已经拥有所有check的权限 false存在一个或多个未获得的权限
     */
    public static boolean checkPermissionsGroup(Context context, String[] permissions) {

        for (String permission : permissions) {
            if (!checkPermission(context, permission)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 申请权限
     *
     * @param activity    Activity
     * @param permissions 权限数组
     * @param requestCode 请求码
     */
    public static void requestPermissions(Activity activity, String[] permissions, int requestCode) {
        // 先检查是否已经授权
        if (!checkPermissionsGroup(activity, permissions)) {
            ActivityCompat.requestPermissions(activity, permissions, requestCode);
        }
    }

    public static void startStoragePermission(FragmentActivity activity, Runnable runnable) {
        XXPermissions.with(activity).permission(Permission.MANAGE_EXTERNAL_STORAGE).request(new OnPermissionCallback() {
            @Override
            public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                if (!allGranted) {
                    return;
                }
                runnable.run();
            }

            @Override
            public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                if (doNotAskAgain) {
                    TipDlg dlg = new TipDlg().setMsg("权限被永久拒绝，请手动授予权限");
                    dlg.setOnDismissListener((isOk, value) -> {
                        if (isOk) {
                            XXPermissions.startPermissionActivity(activity, Permission.MANAGE_EXTERNAL_STORAGE, new OnPermissionPageCallback() {
                                @Override
                                public void onGranted() {
                                    runnable.run();
                                }

                                @Override
                                public void onDenied() {
                                    System.currentTimeMillis();
                                }
                            });
                        }
                    });
                    dlg.show(activity.getSupportFragmentManager(), "");
                }
            }
        });
    }
}
